/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID http service.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2005
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Brano Kemen
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
 
#ifndef __COID_SERVICES_WEBSOLE__HEADER_FILE__
#define __COID_SERVICES_WEBSOLE__HEADER_FILE__


#include "coid/svc/coidsvc.h"
#include "coid/server/logentmgr/client/logentmgr_client.h"


COID_NAMESPACE_BEGIN

////////////////////////////////////////////////////////////////////////////////
class Websole
{
    //NET_OPTIONS: nolog_system_calls
    //NET_RELEASE: 0.1

    LogEntMgr_client    _lem;
    dynarray<logstream*> _ale;
    dynarray<charstr>   _afle;

    enum { SESSION_TIMEOUT = 120 };

public:

    Websole() {}

    net_fnc_s net_meta opcd accept_connect( net_in const charstr& name, net_in const password& pwd, net_out uints* ref );

    net_fnc_s opcd accept_connect_shared()
    {
        return ersDENIED;
    }

    net_fnc_s opcd accept_startup_params( cmd_interface& cmd )
    {
        _cmdin = &cmd;
        _cmdin->request_autodestroy( SESSION_TIMEOUT );
        return 0;
    }

    net_fnc net_meta opcd get_current_pos( net_out uints* ref );

    net_fnc net_meta opcd read( net_inout uints& ref, net_out dynarray<token>& rows, net_out uint& flg );
    net_fnc net_meta opcd command( const charstr& cmd );


protected:
    void format_entry( charstr& dst, const logstream& le );

    static bool is_alphanum(char c)
    { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); }

    static void make_href( token& text, charstr& dst )
    {
        token link = text.cut_left(' ', token::cut_trait_keep_sep_with_source());
        char c = link.last_char();
        if( !is_alphanum(c) && c!='_' )
            link--;
        dst << "<a href=\"" << link << "\" target=\"_blank\">" << link << "</a>";
    }

    static const charstr& check_html( token text, charstr& dst )
    {
        for(;;)
        {
            token head = text.cut_left("<>&hfw", token::cut_trait_keep_sep_with_source());
            dst += head;
            if( text.is_empty() )  break;

            char fc = text.first_char();
            if( (fc == 'w' || fc=='h' || fc=='f') )
            {
                static token http = "http://";
                static token https = "https://";
                static token ftp  = "ftp://";
                static token ftp2  = "ftp.";
                static token www = "www.";

                if( text.begins_with(http) || text.begins_with(https) || text.begins_with(ftp) || text.begins_with(ftp2) || text.begins_with(www) ) {
                    make_href( text, dst );
                    continue;
                }
            }

            ++text;
            if( fc == '>' )
                dst += "&gt;";
            else if( fc == '<' )
                dst += "&lt;";
            else if( fc == '&' )
                dst += "&amp;";
            else
                dst += fc;
        }
        return dst;
    }

protected:
    cmd_interface* _cmdin;
    charstr tmp;
};

COID_NAMESPACE_END

#endif //__COID_SERVICES_WEBSOLE__HEADER_FILE__
